package com.cskaoyan.service.system;

import com.cskaoyan.bean.system.Perm;
import com.cskaoyan.bean.system.Permissions;
import com.cskaoyan.bean.system.Role;
import com.cskaoyan.bean.system.RoleExample;
import com.cskaoyan.bo.system.ListWithNameBo;
import com.cskaoyan.bo.system.PermsUpdateBo;
import com.cskaoyan.bo.system.RoleCreateBo;
import com.cskaoyan.mapper.PermissionMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.vo.system.PermissionsVo;
import com.cskaoyan.vo.system.RoleCreateVo;
import com.cskaoyan.vo.system.RoleListVo;
import com.cskaoyan.vo.system.RoleOptionsVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Transactional
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public ArrayList<RoleOptionsVo> roleOptions() {
        List<RoleOptionsVo> roleOptionsVos = roleMapper.selectAllRoleOptions();
        return (ArrayList<RoleOptionsVo>) roleOptionsVos;
    }


    @Override
    public void permissionInsert(List<Permissions> perms) {

        // 批量插入perm1
        // permissionMapper.insertPerm1(perms);
        for (Permissions perm : perms) {
            permissionMapper.insertPerm1(perm);
        }

        // 插入perm2
        for (Permissions perm : perms) {
            List<Permissions.ChildrenBeanX> permList2 = perm.getChildren();
            // permissionMapper.insertPerm2(permList2, perm.getCid());
            for (Permissions.ChildrenBeanX beanX : permList2) {
                permissionMapper.insertPerm2(beanX, perm.getCid());
            }

            // 批量插入perm3
            for (Permissions.ChildrenBeanX perm2 : permList2) {
                List<Permissions.ChildrenBeanX.ChildrenBean> permList3 = perm2.getChildren();
                // permissionMapper.insertPerm3(permList3, perm2.getCid());
                for (Permissions.ChildrenBeanX.ChildrenBean bean : permList3) {
                    permissionMapper.insertPerm3(bean, perm2.getCid());
                }
            }
        }


        // for (Permissions perm : perms) {
        //     permissionMapper.insertPermWithoutAPIAndPID(perm);
        //     List<Permissions.ChildrenBeanX> permSecond = perm.getChildren();
        //
        //     for (Permissions.ChildrenBeanX perm2 : permSecond) {
        //         permissionMapper.insertPermWithoutAPIWithPID(perm2);
        //         List<Permissions.ChildrenBeanX.ChildrenBean> permThird = perm2.getChildren();
        //
        //         for (Permissions.ChildrenBeanX.ChildrenBean perm3 : permThird) {
        //             permissionMapper.insertPermWithAPIAndPID(perm3);
        //         }
        //     }
        // }


    }

    @Override
    public PermissionsVo rolePermissions(Integer roleId) {
        // List<Perm> permList = permissionMapper.selectAllPerms();
        List<String> assignedPermissions = permissionMapper.selectPermByRid(roleId);

        PermissionsVo.SystemPermissionsBean perm1 = new PermissionsVo.SystemPermissionsBean();
        List<PermissionsVo.SystemPermissionsBean> perm1List = permissionMapper.selectPerm1();
        for (PermissionsVo.SystemPermissionsBean p1 : perm1List) {
            PermissionsVo.SystemPermissionsBean.ChildrenBeanX perm2 = new PermissionsVo.SystemPermissionsBean.ChildrenBeanX();
            List<PermissionsVo.SystemPermissionsBean.ChildrenBeanX> perm2List = permissionMapper.selectPerm2(p1.getId());

            for (PermissionsVo.SystemPermissionsBean.ChildrenBeanX p2 : perm2List) {
                PermissionsVo.SystemPermissionsBean.ChildrenBeanX.ChildrenBean perm3 = new PermissionsVo.SystemPermissionsBean.ChildrenBeanX.ChildrenBean();
                List<PermissionsVo.SystemPermissionsBean.ChildrenBeanX.ChildrenBean> perm3List = permissionMapper.selectPerm3(p2.getId());
                p2.setChildren(perm3List);
            }
            p1.setChildren(perm2List);
        }
        for (PermissionsVo.SystemPermissionsBean p1 : perm1List) {
            List<PermissionsVo.SystemPermissionsBean.ChildrenBeanX> p2L = p1.getChildren();
            for (PermissionsVo.SystemPermissionsBean.ChildrenBeanX p2 : p2L) {
                System.out.println(p2);
            }
        }

        if (assignedPermissions.contains("*")) {
            assignedPermissions = permissionMapper.seletSuperAdminPerms();
        }
        // for (PermissionsVo.SystemPermissionsBean p1 : perm1List) {
        //
        //     List<PermissionsVo.SystemPermissionsBean.ChildrenBeanX> p2List = p1.getChildren();
        //
        //     for (PermissionsVo.SystemPermissionsBean.ChildrenBeanX p2 : p2List) {
        //         List<PermissionsVo.SystemPermissionsBean.ChildrenBeanX.ChildrenBean> p3List = p2.getChildren();
        //         System.out.println(p3List);
        //         for (PermissionsVo.SystemPermissionsBean.ChildrenBeanX.ChildrenBean p3 : p3List) {
        //             System.out.println(p3);
        //             assignedPermissions.add(p3.getApi());
        //         }
        //     }
        // }


        // List<PermissionsVo.SystemPermissionsBean> systemPermissions = new ArrayList<>();
        //
        // for (Perm perm : permList) {
        //     while (perm.getParent() == null) {
        //         PermissionsVo.SystemPermissionsBean perm1 = new PermissionsVo.SystemPermissionsBean();
        //         perm1.setId(perm.getPerm());
        //         perm1.setLabel(perm.getLabel());
        //
        //         List<PermissionsVo.SystemPermissionsBean.ChildrenBeanX> childrenX = new ArrayList<>();
        //
        //         for (Perm perm2 : permList) {
        //             if (perm.getId().equals(perm2.getParent())) {
        //                 PermissionsVo.SystemPermissionsBean.ChildrenBeanX childrenBeanX = new PermissionsVo.SystemPermissionsBean.ChildrenBeanX();
        //                 childrenBeanX.setId(perm2.getPerm());
        //                 childrenBeanX.setLabel(perm2.getLabel());
        //
        //
        //                 List<PermissionsVo.SystemPermissionsBean.ChildrenBeanX.ChildrenBean> children = new ArrayList<>();
        //                 for (Perm perm3 : permList) {
        //                     if (perm2.getId().equals(perm3.getParent())) {
        //                         PermissionsVo.SystemPermissionsBean.ChildrenBeanX.ChildrenBean childrenBean = new PermissionsVo.SystemPermissionsBean.ChildrenBeanX.ChildrenBean();
        //                         childrenBean.setId(perm3.getPerm());
        //                         childrenBean.setLabel(perm3.getLabel());
        //                         childrenBean.setApi(perm3.getApi());
        //                         children.add(childrenBean);
        //                     }
        //                 }
        //                 childrenBeanX.setChildren(children);
        //                 childrenX.add(childrenBeanX);
        //             }
        //         }
        //
        //         // perm1设置children
        //         perm1.setChildren(childrenX);
        //         systemPermissions.add(perm1);
        //     }
        // }
        // System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        // System.out.println(systemPermissions);

        PermissionsVo vo = new PermissionsVo();
        vo.setAssignedPermissions(assignedPermissions);
        vo.setSystemPermissions(perm1List);
        return vo;
    }

    @Override
    public RoleListVo listRoles(ListWithNameBo listWithNameBo) {
        PageHelper.startPage(listWithNameBo.getPage(), listWithNameBo.getLimit());
        RoleExample roleExample = new RoleExample();
        roleExample.setOrderByClause(listWithNameBo.getSort() + " " + listWithNameBo.getOrder());
        RoleExample.Criteria criteria = roleExample.createCriteria();

        if (listWithNameBo.getName() != null && !"".equals(listWithNameBo.getName())) {
            criteria.andNameLike("%" + listWithNameBo.getName() + "%");
        }
        criteria.andDeletedEqualTo(false);

        List<Role> roleList = roleMapper.selectByExample(roleExample);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roleList);
        long total = rolePageInfo.getTotal();

        RoleListVo roleListVo = new RoleListVo();
        roleListVo.setItems(roleList);
        roleListVo.setTotal(total);


        return roleListVo;
    }

    @Override
    public RoleCreateVo createRole(RoleCreateBo roleCreateBo) {
        roleMapper.insertRole(roleCreateBo);
        RoleCreateVo roleCreateVo = roleMapper.selectRoleById(roleCreateBo.getId());
        return roleCreateVo;
    }

    @Override
    public int updateRole(RoleCreateVo roleUpdateBo) {
        int affectedRows = roleMapper.updateRole(roleUpdateBo);
        return affectedRows;
    }

    @Override
    public int deleteRole(Integer id) {
        int affectedRows = roleMapper.deleteByIdLogically(id);
        return affectedRows;
    }

    @Override
    public int updatePerms(PermsUpdateBo permsUpdateBo) {
        // TODO：使用事务包裹
        // 1、先删除permissions表中该角色的所有权限
        permissionMapper.deleteByRoleId(permsUpdateBo.getRoleId());

        // 2、在permissions表中插入该角色新的权限
        int affectedRows = permissionMapper.addPerms(permsUpdateBo.getPermissions(), permsUpdateBo.getRoleId());
        return affectedRows;
    }
}
